package com.yfh.study.manager.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.yfh.data.base.r.Codes;
import com.yfh.data.base.r.R;
import com.yfh.data.base.r.Rs;
import com.yfh.data.base.utils.QfBeanUtils;
import com.yfh.data.system.dataManager.vo.input.MyPlayStatusInput;
import com.yfh.data.system.entity.Play;
import com.yfh.study.manager.entity.PlayLoadOutput;
import com.yfh.study.manager.service.PlayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

import java.util.List;

/**
 * 计划接口
 */
@RestController
@RequestMapping("/play")
@Slf4j
public class MyPlayController {

    @Autowired
    private PlayService playService;
    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/queryAllPlay")
    public R getQueryAllPlay(){
        log.info("[查询所有信息]");
        List<Play> result= playService.queryList();
        if (result == null ||result.isEmpty()) {
            return Rs.createFail(Codes.FAIL);
        }
        return Rs.create(result);
    }

    /**
     * 修改计划表状态
     * @param myPlayStatusInput
     * @return
     */
    @RequestMapping("/updateStatus")
    public R updatePlay(MyPlayStatusInput myPlayStatusInput){
        log.info("[修改表状态] -{}",myPlayStatusInput);
       boolean result =  playService.updateStatus(myPlayStatusInput);
        if (!result) {
            return Rs.createFail(Codes.FAIL);
        }
        return Rs.create(Codes.SUCC);
    }
    @RequestMapping("/addPlay")
    public R updatePlay(String ufunction){
        log.info("[添加计划] -{}",ufunction);
        boolean result =  playService.addPlay(ufunction);
        if (!result) {
            return Rs.createFail(Codes.FAIL);
        }
        return Rs.create(Codes.SUCC);
    }

    /**
     * 删除计划
     * @param id
     * @return
     */
    @RequestMapping("/deletePlay")
    public R deletePlay(Integer id){
        log.info("[删除计划] -{}",id);
        boolean result = playService.deletePlay(id);
        if (!result) {
            log.info("[删除-{}失败]",id);
            return Rs.createFail(Codes.FAIL);
        }
        log.info("[删除-{}-成功]",id);
        return Rs.create("SUCC");
    }

    /**
     * 下载Excel表格
     * @return
     */
    @RequestMapping("/downLoad")
    public void downLoad( HttpServletResponse response ) throws IOException {
        log.info("[下载请求]-下载Excel");
        //文件头预处理
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String fileName= URLEncoder.encode("计划表Excel状态","UTF-8");
        response.setHeader("Content-Disposition","attachment; filename*=UTF-8''"+fileName+".xlsx");
        //获取响应流
        ServletOutputStream outputStream = response.getOutputStream();

        //创建写工作簿  路径  ， 实体类  返回写的工作簿
        ExcelWriterBuilder writeBook = EasyExcel.write(outputStream, PlayLoadOutput.class);
        //获取工作表
        ExcelWriterSheetBuilder sheet = writeBook.sheet();
        //写入  list集合
        List<Play> result= playService.queryList();
        List<PlayLoadOutput> playLoadOutputs = QfBeanUtils.listCopy(result, PlayLoadOutput.class);

        sheet.doWrite(playLoadOutputs);
        log.info("[下载请求]-下载完成");


    }

}
